/**
 * @author SilverBiology, LLC
 * @website http://code.google.com/p/gbif-spreadsheet-processor/
*/
Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial')

GBIF.GeoSpatial.WMSCapabilitiesLayers = function(config){
	
	this.geoMapPanel = new GBIF.GeoSpatial.MapComponent({
			region: 'center'
		,	border: false
	});
	
	this.geoMapPanel.setTitle(this.txtgeoMapPanelTitle);

	this.geoGridPanel = new GBIF.GeoSpatial.WMSCapabilitiesGrid({	
			region: 'west'
		,	split: true
		,	autoScroll: true 
		,	border: false
	});
	
	this.geoGridPanel.on('addLayer', this.loadLayer, this);
	this.geoGridPanel.on('previewLayers', this.showLayers, this);
	
	Ext.apply(this,config,{
			layout: 'border'
		,	border: false
		,	tbar: [{
					xtype: 'label'
				,	style: "padding:0px 5px"	
				,	text: this.txtWMSUrl
			},{
					xtype: 'textfield'  
				,	width: 350
				,	ref: '../url'
				,	vtype: 'url'
				, enableKeyEvents: true 
     	}, {
					text: this.txtLoad
				,	style: "padding:0px 5px"
				,	handler: this.loadUrl
				, scope: this
			}]
		,	proxyUrl: "../resources/api/getcapabilities-proxy.php?url="
		,	items:[this.geoMapPanel,this.geoGridPanel]
	});
	
	GBIF.GeoSpatial.WMSCapabilitiesLayers.superclass.constructor.call(this, config);
};

Ext.extend(GBIF.GeoSpatial.WMSCapabilitiesLayers,Ext.Panel, {

		loadUrl: function(){
			this.clearLayer();
			this.url.setValue(this.url.getValue().trim());
			if (this.url.isValid()) {
				var url = this.url.getValue();
				if (url != '') {
					var url = this.confirmURL(url);
					this.geoGridPanel.store.baseParams = {
						url: url
					};
					this.geoGridPanel.store.load();
				}	else {
					GBIF.GeoSpatial.Notice.msg(this.txtError, this.txtUrlError);
				}
			} else {
				GBIF.GeoSpatial.Notice.msg(this.txtError,"Please enter valid URL.");				
			}
		}
		
	,	confirmURL:function(url){
			var present=false;	
				for (var i = 0; i < url.length; i++) {
					if (url.charAt(i)== "?") {
						present = true;
					}
				}
			if (present) {
				return url;
			} else {
				for (var j = 0; j < url.length; j++) {
					var url1 = url.split("&")[0];
					var url2 = url1.split("service")[0];
					var url3 = url2.split("request")[0];
					var url4 = url3.split("version")[0];
					var url5 = url.split(url4)[1];
//					console.log(url4+"?"+url5);
					return url4+"?"+url5;
				}
			}				
		}	
		
	,	loadLayer:function(layer){
			this.featuredata = [];
			this.layerdata = [];
			var selecteddata = this.geoGridPanel.getSelectionModel().getSelections();
			Ext.each(selecteddata,function(item){
				if(item.data.queryable){
					this.featuredata.push(item);
				} else {
					this.layerdata.push(item);	
//					console.log("this.layerdata",this.layerdata);
				}
			}, this);
		}
		
	,	showLayers: function(layerWin){
			this.clearLayer();
			Ext.each(layerWin,function(maplayer){
				var layer = maplayer.data.layer.clone();
				this.geoMapPanel.map.addLayer(layer);
			}, this);
		}	
		
	,	clearPanel:function(){
			this.url.setValue('');	
			this.geoGridPanel.store.removeAll();
		}
		
	,	clearLayer:function(){
			Ext.each(this.geoMapPanel.map.layers,function(maplayer){
				var layerPresent = (typeof maplayer == 'undefined') ? false : true;
				if (layerPresent) {
					if (!maplayer.isBaseLayer) {
						this.geoMapPanel.map.removeLayer(maplayer);
					}
				}
			}, this);
		}	
		
});